package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
/**
 *
 * @Title: apiSyncDemo
 * @Description:系统API接口开发Demo，重点关注业务逻辑部分
 * @author: LiJuntao
 * @date 2021-07-13 23:58:55
 *
 */

public class apiSyncDemo implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiSyncDemo.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        response.setCharacterEncoding("UTF-8");

        String getMethod = request.getMethod();
        log.debug("getMethod:" + getMethod);


        JSONObject result = new JSONObject();

        String inData = "";
        String outData = "";
        String dataSourceID = "dataSource";

        BufferedReader iny = null;
        if (getMethod.equals("GET")) {
            inData = request.getParameterNames().toString();
            log.debug("原始Get请求参数:" + inData);
        } else {
            InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
            iny = new BufferedReader(reader);
            String line;
            while ((line = iny.readLine()) != null) {
                inData += line;
            }
            log.debug("原始Post请求参数:" + inData);
        }

        log.info("[调用请求信息]:" + inData);

        JSONObject signJson = signUtil.CheckSign(JSONObject.parseObject(inData));
        log.info("[签名验证结果]:");
        log.info(signJson);
        if (signJson != null) {
            return signJson.toString();
        } else {

            try {
                //业务逻辑部分 测试


                result.put("code", 1);
                result.put("msg", "SUCC");
            } catch (Exception e) {
                log.error("", e);

                result.put("code", 1006);
                result.put("msg", "数据处理错误");
            }

        }

        return result.toString();
    }
}